// pages/payfor/detail.js
var PayTotalPrice = ''
var OrderTicketID = ""
var order = "";
var detail = '';
var qrcodeurl ="";
// // coupons = JSON.parse(options.coupons); //优惠卷
// var result = ''
// var lingshilist = ""
var hasChooseSeat = ""
// var plandate = ""
// var showdate = ""
var reqdetailSign = "";
var ticketprice = ""
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var strAppCode = config.SmallApp;
var app = getApp();
var count = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    year: '',
    imgHost: config.imgHost,



    xudian: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ haschu: false })
    wx.showLoading({
      title: '出票中',
      mask: true
    })
    // TODO请求详情
    var that = this;
    hasChooseSeat = JSON.parse(options.hasChooseSeat);
    order = JSON.parse(options.order)
    OrderTicketID = order.OrderTicketID; //订单号
    var detailSign = "strAppCode=" + strAppCode + "OrderTicketID=" + OrderTicketID
    return req.GetSignStr(detailSign).then(res => {
      reqdetailSign = res.data.data
    }).then(() => {

      this.checkchu(this,options);
      // return req.GetOrderDetailList(strAppCode, OrderTicketID, reqdetailSign, app.globalData.token).then(res => {
      //   console.log(res)
      //   detail = res.data.data[0];

      //   console.log(order);
      //   console.log(detail);
      //   var paytimeend = detail.paytime.split('T')[1].split(".")[0]

      //   PayTotalPrice = options.PayTotalPrice;//支付价格
      //   ticketprice = options.ticketprice;
      //   that.setData({
      //     order: order,
      //     detail: detail,
      //     timeyear: detail.paytime.split('T')[0],
      //     timehour: paytimeend,
      //     // dyname: plandate.FilmName,//电影名称
      //     // yyname: plandate.CinemaName, //影院名称
      //     // tingnum: plandate.HallName,//电影厅号      
      //     // showdate: showdate,//开场时间
      //     // chkday: options.chkday,//日期
      //     // payforprice: ticketprice,//电影票钱
      //     // piaojia: plandate.mPriceLowest,//电影票价
      //     zhifuqian: PayTotalPrice,//支付钱
      //     // lingshiqian: options.lingshiqian,//零食钱
      //     chkzuolist: hasChooseSeat,//选择的座位
      //     // year: new Date().getFullYear(),//得到年份
      //     num: hasChooseSeat.length,
      //     danpric: ticketprice / hasChooseSeat.length
      //   })

      //   if (!detail.StateOrder == "3" || !detail.StateOrder == "4" || !detail.StateOrder == "5") {


      //   } else {
      //     var func = function () {
      //       return req.GetOrderDetailList(strAppCode, OrderTicketID, reqdetailSign, app.globalData.token).then(res => {

      //       })
      //     }
      //    setTimeout(func, 60000)
      //   }


      // })
    })






    // coupons = JSON.parse(options.coupons); //优惠卷

    // lingshilist = JSON.parse(options.lingshilist);

    // plandate = JSON.parse(options.plandate);
    // showdate = options.showdate;





    //结束标识符
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.switchTab({
      url: '../index/index'
    })
    // wx.reLaunch({
    //   url: '../index/index'
    // })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  checkchu: function (that,options) {
    console.log("检查出票")
    return req.GetOrderDetailList(strAppCode, OrderTicketID, reqdetailSign, app.globalData.token).then(res => {
      console.log(res)
      detail = res.data.data[0];
      qrcodeurl = res.data.qrcodeurl;
      that.setData({ qrcodeurl})
      console.log(order);
      console.log(detail);
      var paytimeend = detail.paytime.split('T')[1].split(".")[0]

      PayTotalPrice = options.PayTotalPrice;//支付价格
      ticketprice = options.ticketprice;
      that.setData({
        order: order,
        detail: detail,
        timeyear: detail.paytime.split('T')[0],
        timehour: paytimeend,
        // dyname: plandate.FilmName,//电影名称
        // yyname: plandate.CinemaName, //影院名称
        // tingnum: plandate.HallName,//电影厅号      
        // showdate: showdate,//开场时间
        // chkday: options.chkday,//日期
        // payforprice: ticketprice,//电影票钱
        // piaojia: plandate.mPriceLowest,//电影票价
        zhifuqian: "",//支付钱
        // lingshiqian: options.lingshiqian,//零食钱
        chkzuolist: hasChooseSeat,//选择的座位
        // year: new Date().getFullYear(),//得到年份
        num: hasChooseSeat.length,
        danpric: ticketprice / hasChooseSeat.length
      })

      if (detail.StateOrder == "3" || detail.StateOrder == "4" || detail.StateOrder == "5") {
        //查询已经出票
        setTimeout(function () {
          // 放在最后--
          wx.hideLoading();
        }
          , 1600)
          that.setData({haschu:true})
      } else {
        //再次查询
        function callback(){
          return that.checkchu(that,options)
        }
        setTimeout(callback,60000)

      }


    })
  }


})